import { api } from './config'
import type { PageResponse } from './config'

// 员工数据接口
export interface Employee {
  id?: number
  name: string
  gender: 'MALE' | 'FEMALE'
  age: number
  phone: string
  email: string
  hireDate: string
  position: string
  roleId: number
  status: 'ACTIVE' | 'INACTIVE'
  createdTime?: string
  updatedTime?: string
}

// 员工查询条件接口
export interface EmployeeQuery {
  name?: string
  gender?: 'MALE' | 'FEMALE'
  position?: string
  status?: 'ACTIVE' | 'INACTIVE'
  minAge?: number
  maxAge?: number
  hireStartDate?: string
  hireEndDate?: string
  page?: number
  size?: number
  sort?: string
}

// 员工创建/更新数据接口
export interface EmployeeForm {
  name: string
  gender: 'MALE' | 'FEMALE'
  age: number
  phone: string
  email: string
  hireDate: string
  position: string
  roleId: number
  status: 'ACTIVE' | 'INACTIVE'
}

// 批量操作接口
export interface BatchEmployeeRequest {
  employees: EmployeeForm[]
}

export interface BatchDeleteRequest {
  ids: number[]
}

// 员工API服务类
export class EmployeeService {
  private static readonly BASE_URL = '/employees'

  /**
   * 获取员工列表（分页）
   */
  static async getEmployees(query: EmployeeQuery = {}): Promise<PageResponse<Employee>> {
    const response = await api.get<PageResponse<Employee>>(this.BASE_URL, {
      params: query
    })
    return response.data.data
  }

  /**
   * 根据ID获取员工详情
   */
  static async getEmployeeById(id: number): Promise<Employee> {
    const response = await api.get<Employee>(`${this.BASE_URL}/${id}`)
    return response.data.data
  }

  /**
   * 创建新员工
   */
  static async createEmployee(employee: EmployeeForm): Promise<Employee> {
    const response = await api.post<Employee>(this.BASE_URL, employee)
    return response.data.data
  }

  /**
   * 更新员工信息
   */
  static async updateEmployee(id: number, employee: EmployeeForm): Promise<Employee> {
    const response = await api.put<Employee>(`${this.BASE_URL}/${id}`, employee)
    return response.data.data
  }

  /**
   * 删除员工
   */
  static async deleteEmployee(id: number): Promise<void> {
    await api.delete(`${this.BASE_URL}/${id}`)
  }

  /**
   * 条件查询员工
   */
  static async searchEmployees(query: EmployeeQuery): Promise<Employee[]> {
    const response = await api.get<Employee[]>(`${this.BASE_URL}/search`, {
      params: query
    })
    return response.data.data
  }

  /**
   * 批量创建员工
   */
  static async batchCreateEmployees(request: BatchEmployeeRequest): Promise<Employee[]> {
    const response = await api.post<Employee[]>(`${this.BASE_URL}/batch`, request)
    return response.data.data
  }

  /**
   * 批量删除员工
   */
  static async batchDeleteEmployees(request: BatchDeleteRequest): Promise<void> {
    await api.delete(`${this.BASE_URL}/batch`, {
      data: request
    })
  }

  /**
   * 检查手机号是否已存在
   */
  static async checkPhoneExists(phone: string, excludeId?: number): Promise<boolean> {
    const response = await api.get<boolean>(`${this.BASE_URL}/check-phone`, {
      params: { phone, excludeId }
    })
    return response.data.data
  }

  /**
   * 检查邮箱是否已存在
   */
  static async checkEmailExists(email: string, excludeId?: number): Promise<boolean> {
    const response = await api.get<boolean>(`${this.BASE_URL}/check-email`, {
      params: { email, excludeId }
    })
    return response.data.data
  }

  /**
   * 获取员工统计信息
   */
  static async getEmployeeStats(): Promise<{
    total: number
    active: number
    inactive: number
    byPosition: Record<string, number>
    byGender: Record<string, number>
  }> {
    const response = await api.get(`${this.BASE_URL}/stats`)
    return response.data.data
  }

  /**
   * 导出员工数据
   */
  static async exportEmployees(query: EmployeeQuery = {}): Promise<Blob> {
    const response = await api.get(`${this.BASE_URL}/export`, {
      params: query,
      responseType: 'blob'
    })
    return response.data
  }
}

// 导出默认实例
export default EmployeeService
